package com.mysoft.gfyx.common.rpc.opensearch.dto;

import com.alibaba.fastjson.JSONArray;
import com.mysoft.gfyx.common.time.TimeService;
import com.mysoft.gfyx.common.time.impl.TimeServiceImpl;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class SearchSupplierRequestDto extends SearchBaseRequestDto {
    private String keyword;
    private String province_code;
    private String city_code;
    private String industry_category_middle;
    private String project_area;
    private String project_province_code;
    private String project_city_code;
    private String project_start_time;
    private String project_end_time;
    private String area;
    private String created_time;
    private int page = 1;
    private int page_size;

    protected void setFilterByField(String field, String operatorType, Object value) {
        switch (field) {
            case "publish_time":
                TimeService timeService = new TimeServiceImpl();

                SimpleDateFormat normalFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date v = timeService.parseDateStr(value.toString());
                if ("ge".equals(operatorType) || "gt".equals(operatorType)) {
                    this.setProject_start_time(normalFormater.format(v));
                } else if ("le".equals(operatorType) || "lt".equals(operatorType)) {
                    this.setProject_end_time(normalFormater.format(v));
                }
                break;
            case "province_code":
                if (value != null && !value.toString().equals("")) {
                    this.setProvince_code(value.toString());
                    if (this.getArea() == null || this.getArea().equals("")) {
                        this.setArea(value.toString());
                    } else {
                        this.setArea(this.getArea() + "," + value);
                    }
                }
                break;
            case "city_code":
                if (value != null && !value.toString().equals("")) {
                    this.setCity_code(value.toString());
                    if (this.getArea() == null || this.getArea().equals("")) {
                        this.setArea(value.toString());
                    } else {
                        this.setArea(this.getArea() + "," + value);
                    }
                }
                break;
            case "project_time":
                if ("ge".equals(operatorType) || "gt".equals(operatorType)) {
                    this.setProject_start_time(value.toString());
                } else if ("le".equals(operatorType) || "lt".equals(operatorType)) {
                    this.setProject_end_time(value.toString());
                }
                break;
            case "industry_category":
                List<Object> industryCategory = new ArrayList<>();
                if (value instanceof ArrayList<?>) {
                    for (Object o : (List<?>) value) {
                        industryCategory.add(o);
                    }
                }
                //三级
                List<Object> industryCategoryMiddle = new ArrayList<>();
                industryCategory.forEach(p -> {
                    JSONArray item = JSONArray.parseArray(p.toString());
                    industryCategoryMiddle.add(item.get(2));
                });
                String industryCategoryStr = industryCategoryMiddle.stream().map(String::valueOf).collect(Collectors.joining(","));
                this.setIndustry_category_middle(industryCategoryStr);
            default:
                this.dynamicSetProperty(field, value);
        }
    }
}
